package com.wangsd.web.coupon.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wangsd.web.coupon.entity.TOrder;
import com.wangsd.web.coupon.mapper.TOrderMapper;
import com.wangsd.web.coupon.service.TOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author generator
 * @since 2019-09-26
 */
@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements TOrderService {

    @Resource
    TOrderMapper tOrderMapper;

    @Override
    public TOrder selectByOutTradeNo(String outTradeNo) {
        QueryWrapper<TOrder> tOrderQueryWrapper = new QueryWrapper<>();
        tOrderQueryWrapper.select("id,product_id,openid,order_status");
        tOrderQueryWrapper.eq("out_trade_no", outTradeNo);
        TOrder tOrder = tOrderMapper.selectOne(tOrderQueryWrapper);
        return tOrder;
    }

    @Override
    public IPage<TOrder> selectWxOrder(Integer pageNum, Integer pageSize, TOrder tOrder) {
        Page<TOrder> page = new Page<>(pageNum, pageSize);
        QueryWrapper<TOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("openid", tOrder.getOpenid());
        if (tOrder.getOrderStatus() != null) {
            queryWrapper.eq("order_status", tOrder.getOrderStatus());
        }
        queryWrapper.select("id", "created_time", "updated_time", "transaction_id", "product_id", "product_name", "out_trade_no", "order_status",
                "order_total_fee","trade_type","send_type", "real_pay_fee", "time_end", "store_name", "address", "express_name", "express_no", "express_status", "product_pic_url");
        queryWrapper.orderByDesc("created_time");
        IPage<TOrder> productIPage = tOrderMapper.selectPage(page, queryWrapper);
        IPage<TOrder> tOrderIPage = packageData(productIPage);
        return tOrderIPage;
    }

    @Override
    public IPage<TOrder> selectPage(String code, Integer pageNum, Integer pageSize, TOrder tOrder) {
        Page<TOrder> page = new Page<>(pageNum, pageSize);
        QueryWrapper<TOrder> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(code)) {
            queryWrapper.likeRight("code", code);
        }
        if (tOrder.getOrderStatus() != null) {
            queryWrapper.eq("order_status", tOrder.getOrderStatus());
        }
        if (tOrder.getExpressStatus() != null) {
            queryWrapper.eq("express_status", tOrder.getExpressStatus());
        }
        if (tOrder.getCompanyId() != null) {
            queryWrapper.eq("company_id", tOrder.getCompanyId());
        }
        if (tOrder.getBeginTime() != null) {
            queryWrapper.gt("created_time", tOrder.getBeginTime());
        }
        if (tOrder.getEndTime() != null) {
            queryWrapper.lt("created_time", tOrder.getEndTime());
        }
        if (tOrder.getTransactionId() != null) {
            queryWrapper.like("transaction_id", tOrder.getTransactionId());
        }
        List<Integer> integers = Arrays.asList(1, 3);
        queryWrapper.in("order_status", integers);
        queryWrapper.select("id", "created_time","updated_time", "transaction_id", "out_trade_no", "order_status",
                "order_total_fee","send_type","trade_type", "product_id", "product_name", "real_pay_fee", "time_end", "store_name", "address", "express_name", "express_no", "express_status", "product_pic_url");
        queryWrapper.orderByDesc("created_time");
        IPage<TOrder> productIPage = tOrderMapper.selectPage(page, queryWrapper);
        IPage<TOrder> tOrderIPage = packageData(productIPage);
        return tOrderIPage;
    }

    @Override
    public Map selectListStatistics(TOrder params, String code) {
        Map map = tOrderMapper.selectListStatisticsXml(params, code);
        return map;
    }

    @Override
    public List<TOrder> selectAll(TOrder order) {
        QueryWrapper<TOrder> queryWrapper = new QueryWrapper<>();
        if (order.getOrderStatus() != null) {
            queryWrapper.eq("order_status",order.getOrderStatus());
        }
        if(order.getBeginTime()!=null){
            queryWrapper.gt("created_time",order.getBeginTime());
        }
        if(order.getEndTime()!=null){
            queryWrapper.lt("created_time",order.getEndTime());
        }
        List<TOrder> tOrders = tOrderMapper.selectList(queryWrapper);
        return tOrders;
    }

    private IPage<TOrder> packageData(IPage<TOrder> productIPage) {
        List<TOrder> tOrders = productIPage.getRecords();
        if (tOrders != null && tOrders.size() > 0) {
            for (TOrder torder : tOrders) {
                String address = torder.getAddress();
                if (StringUtils.isNotBlank(address)) {
                    JSONObject jsonObject = JSONObject.parseObject(address);
                    StringBuilder addressData = new StringBuilder();
                    addressData.append("客户名称：").append(jsonObject.get("userName")).append(";")
                            .append("邮政编码：").append(jsonObject.get("nationalCode")).append(";")
                            .append("电话：").append(jsonObject.get("telNumber")).append(";")
                            .append("省：").append(jsonObject.get("provinceName")).append(";")
                            .append("市：").append(jsonObject.get("cityName")).append(";")
                            .append("区（县）：").append(jsonObject.get("countyName")).append(";")
                            .append("详细地址：").append(jsonObject.get("detailInfo")).append(";");
                    torder.setAddress(addressData.toString());
                }
            }
        }
        return productIPage;
    }
}
